﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Diagnostics;

namespace CarInfor.UI
{

    /// <summary>
    /// 动作枚举
    /// </summary>
    public enum ActionType
    {
        New,
        Edit,
        View
    }

    /// <summary>
    /// 消息类型
    /// </summary>
    public enum MsgType
    {
        Yearcheck,
        Insurance
    }

    public class Common
    {
        /// <summary>
        /// 添加序号列并获取分页数据
        /// </summary>
        /// <param >DataTable</param>
        /// <returns></returns>
        public static void GetRange(DataTable dt, int startPos, int count)
        {
            DataColumn dc = dt.Columns.Add("序号");
            dc.SetOrdinal(1);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["序号"] = (i + 1).ToString();
            }

            List<DataRow> drList = new List<DataRow>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i < startPos)
                {
                    drList.Add(dt.Rows[i]);
                }
                if (i >= (startPos + count))
                {
                    drList.Add(dt.Rows[i]);
                }
            }
            foreach (DataRow dr in drList)
            {
                dt.Rows.Remove(dr);
            }
        }


        /// <summary>
        /// 检查进程是否已经在运行
        /// </summary>
        /// <returns></returns>
        public static bool IsProcessRunByName(string name)
        {
            Process[] processes = Process.GetProcesses();
            //遍历有相同进程名称正在运行的进程 
            foreach (Process process in processes)
            {
                if (process.ProcessName == name)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 获取明年本月最后一天的日期
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime GetDateNextYearEndDay(DateTime dt)
        {
            string endDT = (dt.Year + 1).ToString() + "-" + dt.Month.ToString() + "-" + DateTime.DaysInMonth(dt.Year + 1, dt.Month);
            return DateTime.Parse(endDT);
        }


        /// <summary>
        /// 获取明年本月提前一天的日期
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime GetDateNextYearDay(DateTime dt)
        {
            return dt.AddYears(1).AddDays(-1);
        }


        /// <summary>
        /// 设置DataGridView样式
        /// </summary>
        /// <param name="dataGridView"></param>
        public static void SetGridViewStyle(System.Windows.Forms.DataGridView dataGridView)
        {
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            dataGridView.AllowUserToAddRows = false;
            dataGridView.AllowUserToResizeRows = false;
            dataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.None;
            dataGridView.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            dataGridView.BorderStyle = System.Windows.Forms.BorderStyle.None;
            dataGridView.ClipboardCopyMode = System.Windows.Forms.DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
            dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
            dataGridViewCellStyle1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
            dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            dataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
            dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
            dataGridView.GridColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255)))));
            dataGridView.MultiSelect = false;
            dataGridView.ReadOnly = false;
            dataGridView.RowHeadersWidth = 20;
            dataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            dataGridView.ColumnHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
            dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(244)))), ((int)(((byte)(244)))), ((int)(((byte)(244)))));
            dataGridView.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle2;
        }
    }
}
